package file

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"iot-base/common/eventtype"
	"iot-base/common/notify"
	"net/http"
	"time"
)

func Test(c *gin.Context) {
	type reqTest struct {
		Id      string `json:"id" form:"id" binding:"required"`
		Aid     string `json:"aid" form:"aid"`
		Aindex  int64  `json:"aindex" form:"aindex"`
		Aflag   string `json:"aflag" form:"aflag"`
		Atype   string `json:"atype" form:"atype"`
		EventId string `json:"eventId" form:"eventId"`
		Params  string `json:"params" form:"params"`
	}
	var test reqTest
	if err := c.ShouldBind(&test); err != nil {
		c.JSON(http.StatusOK, gin.H{"err": "参数错误"})
	}
	if test.Atype == "" {
		ReportAlarm(test.Id, test.Aid, test.Aindex, test.Aflag)
	} else {
		params := make(map[string]interface{})
		if test.Params != "" {
			err := json.Unmarshal([]byte(test.Params), &params)
			if err != nil {
				c.JSON(http.StatusOK, gin.H{"test": "json解析错误!"})
				return
			}
		}
		ReportEvent(test.Id, test.EventId, params)
	}

}
func ReportEvent(tid string, eventId string, params map[string]interface{}) {
	fmt.Println("上报事件")
	notify.RawEvent(tid, eventId, time.Now(), params, nil)
}
func ReportAlarm(tid string, aid string, index int64, flag string) {
	var curAlarm eventtype.Alarm
	curAlarm.TID = tid
	curAlarm.AlarmID = aid
	curAlarm.Mindex = index

	curAlarm.Flag = flag
	curAlarm.Time = time.Now().Unix()
	notify.RawAlarm(&curAlarm)
}
